﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using _0912276_ChatClient.DTO;

namespace _0912276_ChatClient.BO
{
    public class MemberShipBO
    {
        public static Boolean InsertMember(MemberShip member)
        {

            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                employee epl = new employee();
                epl.emp_no = member.Key;
                epl.birth_date = member.DOBirth;
                epl.first_name = member.FirstName;
                epl.last_name = member.LastName;
                if (member.Gender == true)
                    epl.gender = "M";
                epl.gender = "F";
                epl.hire_date = DateTime.Today;
                dse.employees.AddObject(epl);
                dse.SaveChanges();

                salary slr = new salary();
                slr.emp_no = epl.emp_no;
                slr.salary1 = member.Salary;
                slr.from_date = DateTime.Today;
                slr.to_date = new DateTime(2920,1,1);
                dse.salaries.AddObject(slr);
                dse.SaveChanges();
                return true;
            }
        }
        public static void UpdateMember(MemberShip member)
        {
            try
            {
                using (distributedsystemEntities dse = new distributedsystemEntities())
                {
                    employee e = (from p in dse.employees
                                  where p.emp_no == member.Key
                                  select p).First();
                    e.birth_date = member.DOBirth;
                    e.first_name = member.FirstName;
                    e.last_name = member.LastName;
                    if (member.Gender == true)
                        e.gender = "M";
                    e.gender = "F";

                    salary slr = (from p in dse.salaries 
                                  where p.emp_no == member.Key 
                                  orderby p.to_date descending
                                  select p).FirstOrDefault();
                    //kt max dt
                    slr.salary1 = member.Salary;
                    dse.SaveChanges();
                }
            }
            catch (Exception ex)
            { 
            }
        }
        public static void  DeleteMember(int id)
        {
            using (distributedsystemEntities dse = new distributedsystemEntities())
            {
                employee e = (from p in dse.employees
                              where p.emp_no == id
                              select p).First();
                dse.DeleteObject(e);
                dse.SaveChanges();
                //kt salary and relationship
            }
        }
    }
}